package com.xmtlxx.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.system.domain.bo.SysOperatorBo;
import com.xmtlxx.system.domain.vo.SysOperatorVo;
import com.xmtlxx.system.service.ISysOperatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 节点经办人Controller
 *
 * @author xmtlxx
 * @date 2022-04-24
 */
@Validated
@Api(value = "节点经办人控制器", tags = {"节点经办人管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysOperator")
public class SysOperatorController extends BaseController {

    private final ISysOperatorService iSysOperatorService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<SysOperatorVo> initBean(){
        SysOperatorVo vo = new SysOperatorVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询节点经办人列表
     */
    @ApiOperation("查询节点经办人列表")
    @SaCheckPermission("system:sysOperator:list")
    @GetMapping("/list")
    public TableDataInfo<SysOperatorVo> list(@Validated(QueryGroup.class) SysOperatorBo bo, PageQuery pageQuery) {
        return iSysOperatorService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出节点经办人列表
     */
    @ApiOperation("导出节点经办人列表")
    @SaCheckPermission("system:sysOperator:export")
    @Log(title = "节点经办人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysOperatorBo bo, HttpServletResponse response) {
        List<SysOperatorVo> list = iSysOperatorService.queryList(bo);
        ExcelUtil.exportExcel(list, "节点经办人", SysOperatorVo.class, response);
    }

    /**
     * 获取节点经办人详细信息
     */
    @ApiOperation("获取节点经办人详细信息")
    @SaCheckPermission("system:sysOperator:query")
    @GetMapping("/{opid}")
    public R<SysOperatorVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("opid") String opid) {
        return R.ok(iSysOperatorService.queryById(opid));
    }

    /**
     * 新增节点经办人
     */
    @ApiOperation("新增节点经办人")
    @SaCheckPermission("system:sysOperator:add")
    @Log(title = "节点经办人", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysOperatorVo vo) {
        SysOperatorBo bo = new SysOperatorBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysOperatorService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改节点经办人
     */
    @ApiOperation("修改节点经办人")
    @SaCheckPermission("system:sysOperator:edit")
    @Log(title = "节点经办人", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysOperatorVo vo) {
        SysOperatorBo bo = new SysOperatorBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysOperatorService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除节点经办人
     */
    @ApiOperation("删除节点经办人")
    @SaCheckPermission("system:sysOperator:remove")
    @Log(title = "节点经办人", businessType = BusinessType.DELETE)
    @DeleteMapping("/{opids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] opids) {
        return toAjax(iSysOperatorService.deleteWithValidByIds(Arrays.asList(opids), true) ? 1 : 0);
    }
}
